package com.atguigu.gmall.realtime.app.dwd.db;

import com.atguigu.gmall.realtime.utils.MyKafkaUtil;
import com.atguigu.gmall.realtime.utils.MySqlUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @author Felix
 * @date 2023/6/12
 * 互动域：评论事实表
 * 需要启动的进程
 *      zk、kafka、maxwell、DwdInteractionCommentInfo
 */
public class DwdInteractionCommentInfo {
    public static void main(String[] args) {
        //TODO 1.基本环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(4);
        //1.3 指定表执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //TODO 2.检查点相关的设置(略)

        //TODO 3.从kafka的topic_db主题中读取数据  创建动态表
        //3.1 声明消费的主题以及消费者组
        String groupId = "dwd_interaction_comment_info_group";
        //3.2 建表
        tableEnv.executeSql(MyKafkaUtil.getTopicDbDDL(groupId));

        // tableEnv.executeSql("select * from topic_db").print();
        //TODO 4.过滤出评论表数据
        Table commentInfo = tableEnv.sqlQuery("select\n" +
            "    `data`['id'] id,\n" +
            "    `data`['user_id'] user_id,\n" +
            "    `data`['sku_id'] sku_id,\n" +
            "    `data`['appraise'] appraise,\n" +
            "    `data`['comment_txt'] comment_txt,\n" +
            "    ts,\n" +
            "    proc_time\n" +
            "from topic_db\n" +
            "where `table`='comment_info' and `type`='insert'");
        tableEnv.createTemporaryView("comment_info",commentInfo);
        // tableEnv.executeSql("select * from comment_info").print();

        //TODO 5.从MySQL字典表中读取数据  创建动态表
        tableEnv.executeSql(MySqlUtil.getBaseDicLookUpDDL());
        // tableEnv.executeSql("select * from base_dic").print();

        //TODO 6.将评论表和字典表进行关联
        Table joinedTable = tableEnv.sqlQuery("SELECT\n" +
            "    id,\n" +
            "    user_id,\n" +
            "    sku_id,\n" +
            "    appraise,\n" +
            "    comment_txt,\n" +
            "    ts,\n" +
            "    dic.dic_name appraise_name\n" +
            "FROM comment_info AS ci JOIN base_dic FOR SYSTEM_TIME AS OF ci.proc_time AS dic\n" +
            "    ON ci.appraise = dic.dic_code");
        tableEnv.createTemporaryView("joined_table",joinedTable);

        // tableEnv.executeSql("select * from joined_table").print();

        //TODO 7.将关联之后的数据写到kafka主题中
        //7.1 创建动态表和要写入的kafka主题进行映射
        tableEnv.executeSql("CREATE TABLE dwd_interaction_comment (\n" +
            "    id string,\n" +
            "    user_id string,\n" +
            "    sku_id string,\n" +
            "    appraise string,\n" +
            "    comment_txt string,\n" +
            "    ts string,\n" +
            "    appraise_name string,\n" +
            "  PRIMARY KEY (id) NOT ENFORCED\n" +
            ") " + MyKafkaUtil.getUpsertKafkaDDL("dwd_interaction_comment"));
        //7.2 写入
        tableEnv.executeSql("insert into dwd_interaction_comment select * from joined_table");
    }
}
